package cn.doitedu.sometest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class JavaHttpRequestDemo {
    public static void main(String[] args) throws IOException {


        // 构造httpclient的客户端对象
        CloseableHttpClient client = HttpClientBuilder.create()
                .build();


        // 构造一个post请求对象
        HttpPost post = new HttpPost("http://doitedu:8081/api/post/simwords");
        // 设置请求头
        post.addHeader("Content-Type","application/json;utf-8");
        post.addHeader("Accept","application/json;utf-8");

        JSONObject paramObject = new JSONObject();
        paramObject.put("origin","usb 移动固态");


        // 设置请求体
        post.setEntity(new StringEntity(paramObject.toJSONString(),"utf-8"));


        // 发送请求
        CloseableHttpResponse response = client.execute(post);

        // 获取响应体
        HttpEntity entity = response.getEntity();

        // 解析响应体
        String resultJson = EntityUtils.toString(entity);

        JSONObject jsonObject = JSON.parseObject(resultJson);
        String similarWord = jsonObject.getString("similarWord");
        System.out.println(similarWord);




    }
}
